from lexer import tokens
from ply import yacc
from models.qubit import QRegister
from models.deciplex import Deciplex
from decimal import Decimal
from functions.binary import binary_operations

registry = QRegister()
variables = {}
constants = {}

precedence = (
    ('left', 'PLUS'),
    ('left', 'ENTANGLE'),
)

def p_statement(p):
   """
   statement : substitution
   statement : debug
   statement : measurment
   """
   pass


def p_number(p):
   """
   register : NUMBER
   """
   if p[1] in constants:
      p[0] = constants[p[1]]
   else:
      p[0] = constants.setdefault(p[1], registry.add(p[1]))
    
def p_variable(p):
   """
   register : VARIABLE_NAME
   """
   p[0] = variables[p[1]]

def p_single_substitution(p):
   """
   substitution : VARIABLE_NAME OPERATOR_SUB register
   """
   variables[p[1]] = p[3]

def p_debug(p):
   """
   debug : DEBUG_PRINT
   """
   print variables
   print registry

def p_binary(p):
   """
   register : register PLUS register
   register : register ENTANGLE register
   """
   p[0] = registry.apply_function(binary_operations[p[2]],(p[1], p[3]))

def p_check(p):
   """
   measurment : CHECK register
   """
   if p[1] == 'check':
      print registry.check(p[2])

yacc.yacc()
